package com.beauty.dressing.module.market.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.beauty.dressing.R;
import com.beauty.dressing.common.base.BaseActivity;
import com.beauty.dressing.common.bean.AddressManagerBean;
import com.beauty.dressing.common.bean.BaseBean;
import com.beauty.dressing.common.bean.LeaseOrder;
import com.beauty.dressing.common.bean.LeaseOrderResp;
import com.beauty.dressing.common.bean.LeaseReadyPayResultBean;
import com.beauty.dressing.common.bean.SalesOrder;
import com.beauty.dressing.common.bean.User;
import com.beauty.dressing.common.interfaces.OnRecyclerViewItemListener;
import com.beauty.dressing.common.view.LoadingNodataLayout;
import com.beauty.dressing.logic.control.LBController;
import com.beauty.dressing.module.market.adapter.WriteOrderAdapter;
import com.beauty.dressing.module.market.holder.WriteOrderHeaderHolder;
import com.beauty.dressing.module.mine.activity.AddressManageActivity;
import com.beauty.dressing.module.mine.persenter.OrderControl;
import com.beauty.dressing.module.mine.persenter.OrderPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xutingz
 * @E-mail xutz@xianlanba.com
 * @date 2018/5/27
 */
public class WriteOrderActivity extends BaseActivity implements OnRecyclerViewItemListener {
    private WriteOrderAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private WriteOrderHeaderHolder mHeaderHolder;
    private LoadingNodataLayout mNodtaLayout;
    private BaseBean mBaseBean;
    private AddressManagerBean mAddressBean;
    private TextView total_price_txt;
    LeaseOrderResp bean = new LeaseOrderResp();

    public static void startActivity(Context context, BaseBean baseBean, AddressManagerBean address) {
        Intent intent = new Intent(context, WriteOrderActivity.class);
        intent.putExtra("baseBean", baseBean);
        intent.putExtra("addressBean", address);
        context.startActivity(intent);
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_write_order);
        Intent intent = getIntent();
        if (intent.hasExtra("baseBean")) {
            mBaseBean = (BaseBean) intent.getSerializableExtra("baseBean");
            mAddressBean = (AddressManagerBean) intent.getSerializableExtra("addressBean");
        }
    }

    @Override
    public void findView() {
        findViewById(R.id.left_img_btn).setOnClickListener(this);
        findViewById(R.id.payment_lease).setOnClickListener(this);
        TextView title = findViewById(R.id.title_txt);
        title.setText("填写订单");
        mRecyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new WriteOrderAdapter(context);
        mHeaderHolder = new WriteOrderHeaderHolder(context, mInflater, mRecyclerView);
        mHeaderHolder.setOnRecyclerViewItemListener(this);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.addHeaderView(mHeaderHolder.itemView);
        total_price_txt = findViewById(R.id.total_price_txt);

    }

    @Override
    public void initData() {
        double total_price;
        List<BaseBean> list = new ArrayList<>();
        if (mBaseBean instanceof SalesOrder) {
            SalesOrder salesOrder = (SalesOrder) mBaseBean;
            list.addAll(salesOrder.getOrderList());
        } else if (mBaseBean instanceof LeaseOrder) {
            LeaseOrder leaseOrder = (LeaseOrder) mBaseBean;
            list.addAll(leaseOrder.getOrderList());
            //添加数据
            bean = new LeaseOrderResp();
            bean.defaAddress = mAddressBean;
            bean.goods = leaseOrder;
            mHeaderHolder.bindData(bean);
            total_price = Double.valueOf(bean.goods.price) + Double.valueOf(bean.goods.deposit) + Double.valueOf(bean.goods.leasePostage);
            total_price_txt.setText("¥" + total_price);
        }
        if (!list.isEmpty()) {
            mAdapter.setChild(list);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_img_btn:
                finish();
                break;
            case R.id.payment_lease:
                User user = LBController.getInstance().getCacheManager().getLoginUser();
                String take_way = null;
                if (mHeaderHolder.itemView.findViewById(R.id.user_address_ll).getVisibility() == View.GONE)
                    take_way = "1";
                else
                    take_way = "0";
                orderPresenter.leaseReadyPay(user.member_id, user.token, bean.goods.lease_id
                        , bean.goods.order_id, take_way, bean.defaAddress.getName()
                        , bean.defaAddress.getAddress(), bean.defaAddress.getPhone());
                break;
        }

    }

    OrderPresenter orderPresenter = new OrderPresenter(new OrderControl() {
        @Override
        public void onLeaseReadyPay(boolean isSuccess, int code, String message, LeaseReadyPayResultBean data) {
            if (isSuccess && data != null) {
                startActivity(new Intent(context, MethodPaymenteActivity.class).putExtra("data", data));
            }
        }
    });

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 666 && data != null) {
            bean.defaAddress = (AddressManagerBean) data.getSerializableExtra("AddressBean");
            mHeaderHolder.bindData(bean);
        }
    }

    @Override
    public void onItemViewOnClick(int type, int position) {
        switch (type) {
            case R.id.user_address_ll:
                startActivityForResult(new Intent(this, AddressManageActivity.class).putExtra("type", "order"), 666);
                break;
        }
    }
}
